/*
 * @Author: JohnYang
 * @Date: 2020-09-24 22:19:29
 * @LastEditors: Zhang Yingying
 * @LastEditTime: 2020-11-23 22:31:28
 */

import _ from "lodash";

export const validRequired = {
  required: true,
  type: "string",
  message: "请填写",
  trigger: ["blur", "change"],
  registerError(msg) {
    this.message = msg;
    return _.cloneDeep(this);
  },
  registerType(type) {
    this.type = type;
    return _.cloneDeep(this);
  }
};

export const validMobilePhone = {
  error: null,
  registerError(msg) {
    this.message = msg;
    this.error = new Error(msg);
    return _.cloneDeep(this);
  },
  validator(rule, value, callback) {
    if (!value) {
      callback();
      return;
    }
    if (!/1[345789]\d{9}/.test(value)) {
      this.error == null && (this.error = new Error(this.message));
      callback(this.error);
    } else {
      callback();
    }
  },
  message: "手机号格式不正确",
  trigger: ["blur", "change"]
};

export const validEmail = {
  error: null,
  registerError(msg) {
    this.message = msg;
    this.error = new Error(msg);
    return _.cloneDeep(this);
  },
  validator(rule, value, callback) {
    if (!value) {
      callback();
      return;
    }
    if (
      !/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(
        value
      )
    ) {
      this.error == null && (this.error = new Error(this.message));
      callback(this.error);
    } else {
      callback();
    }
  },
  message: "邮箱格式不正确",
  trigger: ["blur", "change"]
};
